<?php

Raise::load('core.RaiseObject');
Raise::load('db.core.RaiseDbModel');
Raise::load('db.sql.RaiseDbSqlIdentifier');
Raise::load('db.sql.RaiseDbSqlWhere');

/**
 * RaiseDbSqlJoin class
 * represents a JOIN statement in the SQL code
 *
 * @author Sam-Mauris Yong / hellclanner at live dot com
 * @license http://www.opensource.org/licenses/bsd-license New BSD License
 * @package phpraise.db.sql
 * @since 1.2
 */
class RaiseDbSqlJoin extends RaiseObject {

    /**
     * Name of the RaiseDbModel to join with
     * @var RaiseDbModel
     */
    private $model;

    /**
     * Alias of the model name
     * @var string
     */
    private $alias;

    /**
     * The type of join, e.g. INNER, LEFT, RIGHT, OUTER
     * @var string
     */
    private $type;

    /**
     * The condition for this join
     * @var RaiseDbSqlWhere
     */
    private $on;

    function __construct($model, $on = null) {
        $this->model($model);
        $this->on($on ? $on : new RaiseDbSqlWhere());
    }

    /**
     * Get or set the model to join with
     * @param RaiseDbModel $m (optional) The model to join with
     * @return RaiseDbModel
     */
    public function model($m = null){
        if(func_num_args() == 1){
            $this->model = $m;
        }
        return $this->model;
    }

    /**
     * Get or set the alias for the table name
     * @param string $a (optional) Set the alias
     * @return string
     */
    public function alias($a = null){
        if(func_num_args() == 1){
            $this->alias = $a;
        }
        return $this->alias;
    }

    /**
     * Get or set the type of JOIN
     * @param string $t (optional) Set a new type of JOIN
     * @return string
     */
    public function type($t = null){
        if(func_num_args() == 1){
            $this->type = $t;
        }
        return $this->type;
    }

    /**
     * Get or set the condition(s) for this join
     * @param RaiseDbSqlWhere $c (optional) New condition
     * @return RaiseDbSqlWhere
     */
    public function on($c = null){
        if(func_num_args() == 1){
            $this->on = $c;
        }
        return $this->on;
    }
    
    public function __toString() {
        $q = 'JOIN ';
        if($this->type()){
            $q = $this->type() . ' ' . $q;
        }
        
        $model = $this->model;

        $name = '';

        if($model instanceof RaiseDbSqlIdentifier){
            $name = $model->__toString();
        }elseif($model instanceof RaiseDbModel){
            $model = $model->table();
            $name = RaiseDbSqlIdentifier::field($model, $this->model->connection());
        }else{
            $name = $model;
        }

        $q .= $name;

        if($this->alias){

            if($this->alias instanceof RaiseDbSqlIdentifier){
                $name = $this->alias->__toString();
            }elseif($this->alias instanceof RaiseDbModel){
                $model = $this->alias->table();
                $name = RaiseDbSqlIdentifier::field($this->alias, $this->model->connection());
            }else{
                $name = $this->alias;
            }
            if(is_object($name)){
                $name = $name->__toString();
            }
            $q .= ' AS ' . $name;
        }
        
        $q .= ' ON ' . $this->on()->__toString();
        return $q;
    }

}